package cn.itcast.ssm.Contoller;


import cn.itcast.ssm.Domain.resultInfo;
import cn.itcast.ssm.Domain.word;
import cn.itcast.ssm.Service.imgService;
import cn.itcast.ssm.Service.wordService;
import cn.itcast.ssm.util.DateUtil;
import cn.itcast.ssm.util.MyBatisUtil;
import cn.itcast.ssm.util.MyhttpUtil;

import com.google.gson.JsonObject;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/img")
public class imgController {
    @Autowired
    private imgService iservice;

    @RequestMapping("/getImg")
    public @ResponseBody
    List getImg(String keywords) {

        if (keywords == null || "".equals(keywords)) {
            keywords = "风景";
        }
        String encode = null;
        try {
            encode = URLEncoder.encode(keywords, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        ArrayList imgList = iservice.getImgList(encode);
        return imgList;
    }

    @RequestMapping("/getCYJL")
    public @ResponseBody
    Object getCYJL(String keywords) {

        if (keywords == null || "".equals(keywords)) {
            return null;
        }
        String encode = null;
        try {
            encode = URLEncoder.encode(keywords, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String s = iservice.getCYJL(encode);
        resultInfo resultInfo = new resultInfo();
        resultInfo.setMessage(s);
        return resultInfo;
    }

    @RequestMapping("/chat")
    public @ResponseBody
    Object chat(String keywords, String key) throws UnsupportedEncodingException {
        if (keywords != null && !"".equals(keywords)) {
            String encode = URLEncoder.encode(keywords, "utf-8");
            String s = MyhttpUtil.chatRoubt(encode, key);
//            String utf2GbkString = new String(s.getBytes("GBK"),"utf-8");
            System.out.println(s);

            resultInfo resultInfo = new resultInfo();
            resultInfo.setMessage(s);
            return resultInfo;
        }
        return "";
    }

    @RequestMapping("/addword")
    public @ResponseBody
    Object
    addword(String word) {
        if (word != null && !"".equals(word)) {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            wordService mapper = sqlSession.getMapper(wordService.class);
            word word1 = new word();
            word1.setDate(DateUtil.getDate());
            word1.setText(word);
            mapper.insertWord(word1);
            sqlSession.commit();
            sqlSession.close();

        }
        return new resultInfo(1, "添加成功", null);

    }

    @RequestMapping("/findwords")
    public @ResponseBody
    Object
    findwords() {

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        wordService mapper = sqlSession.getMapper(wordService.class);
        List <word> getwords = mapper.getwords();
        sqlSession.close();

        return new resultInfo(1, "成功", getwords);

    }

}
